﻿/* ==============================================================================
 * 功能描述：RoleInfoPanel  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/10 19:47:36
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using ZZQFrameWork;

public class RoleInfoPanel : MonoBehaviour
{
    private CharacterModule characterModule
    {
        get { return ModuleManager.Instance.Get<CharacterModule>(); }
    }

    private RoleDataVO roleDataVo;

    private Slider hpSlider;
    private Slider mpSlider;
    //private Slider goldSlider;//金钱

    private Text hpText;
    private Text mpText;
    private Text goldText;

    private Text atkText;
    private Text defText;
    private Text magicAtkText;

    /// <summary>
    /// 基础属性集合
    /// </summary>
    private Text[] attrItems;

    /// <summary>
    /// 魔法属性集合
    /// </summary>
    private RoleInfoPanelMagicAttrItem[] magicAttrItems;

    void Awake()
    {
        hpSlider = transform.GetComponentByPath<Slider>("PlayerState/Hp/Slider");
        mpSlider = transform.GetComponentByPath<Slider>("PlayerState/Mp/Slider");
        //goldSlider = transform.GetComponentByPath<Slider>("PlayerState/Gold/Slider");
        hpText = transform.GetComponentByPath<Text>("PlayerState/Hp/Text");
        mpText = transform.GetComponentByPath<Text>("PlayerState/Mp/Text");
        goldText = transform.GetComponentByPath<Text>("PlayerState/Gold/Text");
        atkText = transform.GetComponentByPath<Text>("PlayerSymbol/AttackValue");
        defText = transform.GetComponentByPath<Text>("PlayerSymbol/DefValue");
        magicAtkText = transform.GetComponentByPath<Text>("PlayerSymbol/MagicValue");

        attrItems = transform.Find("BaseAttrGrid").GetComponentsInChildren<Text>(true);
        magicAttrItems = transform.Find("MagicAttrGrid").GetComponentsInChildren<RoleInfoPanelMagicAttrItem>(true);
    }

    void OnEnable()
    {
        EventDispatcher.AddListener<int>(Events.RoleEvent.OnUpdateLv, OnUpdateLv);
        EventDispatcher.AddListener(Events.CharacterEvent.OnRefreshPlayerInfoPanel, RefreshPanel);
        RefreshPanel();
    }

    void OnDisable()
    {
        EventDispatcher.RemoveListener<int>(Events.RoleEvent.OnUpdateLv, OnUpdateLv);
        EventDispatcher.RemoveListener(Events.CharacterEvent.OnRefreshPlayerInfoPanel, RefreshPanel);
    }

    /// <summary>
    /// 角色升级需要刷新界面
    /// </summary>
    /// <param name="obj"></param>
    private void OnUpdateLv(int obj)
    {
        RefreshPanel();
    }

    private void RefreshPanel()
    {
        //Debug.Log(characterModule);
        roleDataVo = characterModule.mainRole;
        hpSlider.value = roleDataVo.hp/(float) roleDataVo.maxHp;
        mpSlider.value = roleDataVo.mp / (float)roleDataVo.maxMp;
        //strengSlider.value = roleDataVo.streng / (float)roleDataVo.maxStreng;
        hpText.text = roleDataVo.hp +"/"+roleDataVo.maxHp;
        mpText.text = roleDataVo.mp + "/" + roleDataVo.maxMp;
        goldText.text = roleDataVo.gold.ToString();

        atkText.text = roleDataVo.minAtk + "-" + roleDataVo.maxAtk;
        defText.text = roleDataVo.def.ToString();
        magicAtkText.text = roleDataVo.magicAtk.ToString();

        SetAttrData();
        SetMagicAttrData();
    }

    /// <summary>
    /// 设置属性数据
    /// </summary>
    private void SetAttrData()
    {
        for (int i = 0; i < attrItems.Length; i++)
        {
            Enum_AttrType type = (Enum_AttrType) i;
            attrItems[i].name = type.ToString();
            AttrData data = roleDataVo.GetAttrDataByAttrType(type);
            attrItems[i].text = data.attrName + ":" + data.attrValue;
        }
    }

    /// <summary>
    /// 设置魔法属性数据
    /// </summary>
    private void SetMagicAttrData()
    {
        for (int i = 0; i <magicAttrItems.Length ; i++)
        {
            Enum_AttrType type = magicAttrItems[i].magicAttrType;
            //Debug.Log("即将查找属性："+type);
            AttrData data = roleDataVo.GetAttrDataByAttrType(type);
            //Debug.Log(data.attrName+"==="+data.attrValue);
            magicAttrItems[i].Value = data.attrValue;
        }
    }

}